
public class e092 {
    public static void main(String[] args) {
        int num = 0;
        long sTime = System.currentTimeMillis();
        for (long i = 1; i < 10000000; i++) {
            if (stuck89(i)) {
                num++;
            }
        }
        System.out.println(num);
        long fTime = System.currentTimeMillis();
        System.out.println("Time: " + (fTime - sTime) + "ms");
    }

    static synchronized boolean stuck89(long n) {
        long i = n;
        boolean is89 = false;
        while (true) {
            i = prop(i);
            if (i == 89) {
                return true;
            } else if (i == 1) {
                return false;
            }
        }
    }

    static long prop(long n) {
        String s = String.valueOf(n);
        long sum = 0;
        for (int j = 0; j < s.length(); j++) {
            long k = Long.parseLong(String.valueOf(s.charAt(j)));
            sum += k * k;
        }
        return sum;
    }
}